package com.burry.services;

import java.util.List;
import java.util.Map;

import applelew.util.Debug;

import com.burry.bo.SelectItems;
import com.burry.db.SQLMachine;
import com.burry.util.MapList;

public class GoodService {

	public static int counter = 0;

	public String goodCataInit(MapList ml) {
		StringBuffer sb = new StringBuffer();

		//Debug.logInfo("allSpList ==> " + CatalogService.allSpList);
		List root = (List) ml.get("-1");
		for (int i = 0; i < root.size(); i++) {

			Map tmp = (Map) root.get(i);
			String upper = (String) tmp.get("fa_code");
			String code = (String) tmp.get("cata_code");
			String name = (String) tmp.get("cata_name");
			/**
			 * TreeNode(pid,name,id)
			 */
			sb.append("aryPType[" + counter + "] = new TreeNode('" + upper
					+ "','" + name + "','" + code + "');");
			counter++;

			String result = getStrByCode(ml, code);
			sb.append(result);

		}
		sb.append("var countPType = " + (counter) + ";");
		counter = 0;

		return sb.toString();
	}

	private String getStrByCode(MapList ml, String code) {
		StringBuffer sb = new StringBuffer();

		List sonList = (List) ml.get(code);
		
		if (sonList == null)
			return sb.toString();

		for (int j = 0; j < sonList.size(); j++) {

			Map t = (Map) sonList.get(j);
			String up = (String) t.get("fa_code");
			String co = (String) t.get("cata_code");
			String na = (String) t.get("cata_name");

			sb.append("aryPType[" + counter + "] = new TreeNode('" + up + "','"
					+ na + "','" + co + "');");
			counter++;

			String result = getStrByCode(ml, co);
			sb.append(result);
		}

		return sb.toString();
	}

	/**
	 * @param inputGoodId
	 *            ; 商品编号
	 * @param inputFea
	 *            ; 商品特性
	 * @param cname
	 *            ; 商品名称
	 * @param goodIntro
	 *            ; 商品简介
	 * @param goodDes
	 *            ; 商品详细介绍
	 * @param goodDesign
	 *            ; 商品设计
	 * @param goodSize
	 *            ; 商品尺码
	 * @return
	 */
	public Map addAGood(String inputGoodId, String inputFea, String cname,
			String goodIntro, String goodDes, String goodDesign,
			String goodSize, String cPrice, String oPrice, String mPrice,
			String sPrice,String goodTag,String goodSell) {
		String sql = "insert into bu_good_info(good_id,good_name,good_feature,good_intro,good_desc,good_work,good_size,current_price,ori_price,market_price,same_market_price,good_tag,good_sell) values('"
				+ inputGoodId
				+ "','"
				+ cname
				+ "','"
				+ inputFea
				+ "','"
				+ goodIntro
				+ "','"
				+ goodDes
				+ "','"
				+ goodDesign
				+ "','"
				+ goodSize
				+ "',"
				+ cPrice
				+ ","
				+ oPrice
				+ ","
				+ mPrice
				+ ","
				+ sPrice + " , '"+goodTag+"','"+goodSell+"')";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.updateResult(sql);
	}

	public List getAllGood() {
		String sql = "select a.*,b.cata_id from bu_good_info a left join bu_good_cata_relation b on a.good_id = b.good_id";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.getResultSet(sql);
	}

	public List getAllSubColor(String goodId) {
		String sql = "select * from bu_good_sub where good_id = '" + goodId
				+ "'";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.getResultSet(sql);
	}
	
	/**
	 * 
	 * @param goodId | 分割
	 * @return
	 */
	public List getSubColorInGoodIds(String goodId) {
		if(goodId == null)
			return null;
		String[] goods = goodId.split("\\|");
		StringBuffer goodStr = new StringBuffer();
		//Debug.logInfo("g lenght ==> " + goods.length);
		for(int i=0;i<goods.length;i++){
			goodStr.append("'" + goods[i] + "',");
		}
		goodStr.deleteCharAt(goodStr.length()-1);
		String sql = "select a.*,b.color_id,b.color_name,b.prd_color_pic,b.prd_list_pic from bu_good_info a left join bu_good_sub b on a.good_id = b.good_id where a.good_id  in (" + goodStr.toString()
				+ ") limit 0 ,4";
		
		Debug.logInfo("get sub color sql ===> " + sql);
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.getResultSet(sql);
	}

	public List getAllSuitColor(String goodId, String colorId) {
		String sql = "select * from bu_tcolor_pic where good_id = '" + goodId
				+ "' and color_id = '" + colorId + "'";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.getResultSet(sql);
	}

	public List getAllColorPic() {
		String sql = "select a.good_id,b.color_id,b.prd_list_pic from bu_good_info a left join bu_good_sub b on a.good_id = b.good_id";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.getResultSet(sql);
	}

	public Map addSomeLike(String goodId, String likeId) {
		String sql = "update bu_good_info set like_id = '" + likeId
				+ "' where good_id = '" + goodId + "'";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.updateResult(sql);
	}

	public Map addSomePro(String goodId, String proId) {
		String sql = "update bu_good_info set fix_id = '" + proId
				+ "' where good_id = '" + goodId + "'";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.updateResult(sql);
	}
	
	public Map delGood(String goodId){
		//TODO 补全删除其他数据
		String sql = "delete from bu_good_info where good_id = '"+goodId+"'";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.updateResult(sql);
	}
	
	public Map addGoodCataInfo(String goodId,String cataId){
		String sql = "insert into bu_good_cata_relation(good_id,cata_id) values('"+goodId+"','"+cataId+"')";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.updateResult(sql);
	}
	
	/**
	 * @param cataId
	 * @return
	 */
	public List getGoodByCata(String cataId){
		CatalogService cs = new CatalogService();
		String cata = cs.getChildStr(cataId);
		String sql = "select * from bu_good_info where good_id in (select good_id from bu_good_cata_relation where cata_id in ("+cata+"))";
		Debug.logInfo("getGoodByCata sql ==> " + sql);
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.getResultSet(sql);
		
	}
	
	/**
	 * select b.color_id,b.color_name,b.prd_list_pic,a.* from (select * from bu_good_info where good_id in (select good_id from bu_good_cata_relation where cata_id in ('7200','7600','7606','7601'))) a left join bu_good_sub b on a.good_id = b.good_id; 
	 * @param cataId
	 * @return
	 */
	public List getAllGoodSubByCata(String cataId){
		String sql = "select b.color_id,b.color_name,b.prd_list_pic,a.* from (select * from bu_good_info where good_id in (select good_id from bu_good_cata_relation where cata_id = '"+cataId+"')) a left join bu_good_sub b on a.good_id = b.good_id";
		Debug.logInfo("getGoodByCata sql ==> " + sql);
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.getResultSet(sql);
	}
	
	public List getGoodById(String goodId){
		String sql = "select a.*,b.color_id,b.color_name,b.prd_color_pic,b.prd_list_pic,b.prd_check_pic from bu_good_info a left join bu_good_sub b on a.good_id = b.good_id where a.good_id = '"+goodId+"' ";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.getResultSet(sql);
	}
	
	public SelectItems getSelectGood(String size,String num,String goodId,String colorId){
		SelectItems si = new SelectItems();
		String sql = "select a.*,b.color_id,b.color_name,b.prd_color_pic,b.prd_list_pic,b.prd_check_pic,b.prd_order_pic from bu_good_info a left join bu_good_sub b on a.good_id = b.good_id where a.good_id = '"+goodId+"' and b.color_id = '"+colorId+"'";
		SQLMachine sqlM = new SQLMachine("default");
		List resultList = sqlM.getResultSet(sql);
		Map goodMap = (Map)resultList.get(0);
		String goodName = (String)goodMap.get("good_name");
		Double cPrice = (Double)goodMap.get("current_price");
		Double oPrice = (Double)goodMap.get("ori_price");
		String gSize = (String)goodMap.get("good_size");
		String cName = (String)goodMap.get("color_name");
		String gSell = (String)goodMap.get("good_sell");
		String gCheckPic = (String)goodMap.get("prd_check_pic");
		String gListPic = (String)goodMap.get("prd_list_pic");
		String gOrderPic = (String)goodMap.get("prd_order_pic");
		SizeService ss = new SizeService();
		List sizeList = ss.getSizeDetail(gSize);
		List suiteList = getAllSuitColor(goodId,colorId);
		si.setColorId(colorId);
		si.setColorName(cName);
		si.setCurrentPrice(cPrice);
		si.setOriPrice(oPrice);
		si.setNum(num);
		si.setSize(size);
		si.setGoodName(goodName);
		si.setGoodSize(sizeList);
		si.setPs(suiteList);
		si.setGoodSell(gSell);
		si.setCheckPic(gCheckPic);
		si.setListPic(gListPic);
		si.setQuickPic(gOrderPic);
		
		return si;
	}
}
